#!/usr/bin/env python

# *************************************************************************

# Copyright 2010 Texas A&M University, 
#                College Station, Texas
#                Contact: R Crosby (rwc@cs.tamu.edu)
#
# Copyright 2003, 2004 by BiRC -- Bioinformatics Research Center
#                University of Aarhus, Denmark
#                Contact: Thomas Mailund <mailund@birc.dk>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

# *************************************************************************

import subprocess
import sys

# *************************************************************************

class QSubScript:
    """Generate and submit a QSub Script"""

    qsubpgm = './qsub'

    # *********************************************************************

    def __init__ ( self, filename, jobname, queue ):
        """Constructor creating the file"""
    
        self.filename = filename
        self.jobname = jobname

        self.scfp = open(filename, 'w')

        self.WriteCmd('-q', queue)
        self.WriteCmd('-N', jobname)

    # *********************************************************************

    def Close ( self ):
        """Close the script file"""

        self.scfp.close()

    # *********************************************************************

    def Submit(self):
        """Submit the script to torque"""

        self.scfp.close()

        try:

            proc = subprocess.Popen([self.qsubpgm, self.filename], stdout=subprocess.PIPE, stderr=subprocess.PIPE)

        except OSError, err:
            print >> sys.stderr, "Error submitting script, errno", err.errno, err.strerror
            sys.exit(2)

        so, se = proc.communicate();
      
        if proc.returncode:
            print >> sys.stderr, "Return code from script submit:", proc.returncode
            print >> sys.stderr, "Stdout:"
            print >> sys.stderr, so
            print >> sys.stderr, "Stderr:"
            print >> sys.stderr, se
            sys.exit(2)

        return so.strip()

    # *********************************************************************

    def WriteTx ( self, text ):
        """Write arbitrary text to the script without a carriage return"""

        print >>self.scfp, text,

    # *********************************************************************

    def WriteLn ( self, line ):
        """Write an arbitrary line to the script"""

        print >>self.scfp, line

    # *********************************************************************

    def WriteCmd(self, cmd, data):
        """Write a torque command to the script"""

        print >>self.scfp, '$PBS', cmd, data

# *************************************************************************

if __name__ == "__main__":
    print 'Needs to be run from QQFront main'
